package com.frxuexi.park2210.park

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.DatePicker
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.ColorBean
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.net.Const
import com.frxuexi.park2210.R
import com.frxuexi.park2210.adapter.ColorAdapter
import com.frxuexi.park2210.adapter.ParkImgAdapter
import com.frxuexi.park2210.databinding.ActivityParkAddBinding
import com.google.gson.Gson
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import java.time.Year
import java.util.Calendar

@Route(path = "/app/ParkAddActivity")
@AndroidEntryPoint
class ParkAddActivity : BaseActivity<ActivityParkAddBinding,ParkViewModel>() {
    val colorAdapter by lazy { ColorAdapter() }
    val parkImgAdapter by lazy { ParkImgAdapter() }
    override fun initData() {
        lifecycleScope.launch {
            vm.colorSuc.collect{
                if(it.isNotEmpty()) {
                    val colorBean = Gson().fromJson(it, ColorBean::class.java)
                    colorAdapter.addData(colorBean)
                    colorAdapter.notifyDataSetChanged()
                }
            }
        }

        /*lifecycleScope.launch {
            vm.addParkSuc.collect{
                if(it.isNotEmpty()) {
                    ToastUtils.showLong("添加成功")
                    finish()
                }
            }
        }*/

        vm.addParkSuc.observe(this){
            ToastUtils.showLong(it)
            finish()
        }



    }

    override fun initView() {
        binding.hedferParkadd.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("申请车位")
        }

        binding.tvUsername.setText(SPUtils.getInstance().getString(Const.UNAME,""))
        binding.tvAddtime.setText(vm.getDate(System.currentTimeMillis()))

        binding.tvParkstart.setText(vm.getDate(System.currentTimeMillis()))
        binding.tvParkend.setText(vm.getDate(System.currentTimeMillis()))

        binding.rvColor.apply {
            adapter=colorAdapter
            layoutManager=LinearLayoutManager(this@ParkAddActivity,RecyclerView.HORIZONTAL,false)
        }

        //选择颜色事件
        colorAdapter.setOnItemClickListener { adapter, view, position ->
            binding.tvSelcolor.setBackgroundColor(Color.parseColor(colorAdapter.getItem(position).colorValue))
        }

        binding.starttime.setOnClickListener {
            showDateDialog(binding.tvParkstart)
        }

        binding.endtime.setOnClickListener {
            showDateDialog(binding.tvParkend)
        }

        binding.upimg.setOnClickListener {
            PictureSelector.create(this,PictureSelector.SELECT_REQUEST_CODE).selectPicture()
        }

        binding.rvCarimg.apply {
            adapter=parkImgAdapter
            layoutManager=GridLayoutManager(this@ParkAddActivity,4)
        }

        vm.getColors()

        binding.btnAddpark.setOnClickListener {
            val map = mutableMapOf<String, Any>()
            map["parkEnd"]=binding.tvParkend.text.toString()
            map["parkName"]=binding.tvCardno.text.toString()
            map["parkStart"]=binding.tvParkstart.text.toString()
            map["parkTime"]=vm.getDateTime(System.currentTimeMillis())
            map["parkUser"]=SPUtils.getInstance().getInt(Const.UID)
            vm.addPark(map)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            val bean =
                data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            parkImgAdapter.apply {
                addData(bean?.path!!)
                notifyDataSetChanged()
            }
        }
    }



}